package com.koron.css2.systemManage.service;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.common.utils.HttpUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.systemManage.bean.UserRangeBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NewDataRangeService implements IDataRangeService{
	
	private static Logger log = LogManager.getLogger(NewDataRangeService.class);

	@Override
	public List<UserRangeBean> getUserRangeValueList(String userCode, String appId, String catalogue) {
		if(StringUtils.isEmpty(userCode)) {
			throw new IllegalArgumentException("_userCode 不能为空");
		}
		Map<String, String> param = new HashMap<String, String>();
		Map<String, String> body = new HashMap<String, String>();
		body.put("user", userCode);
		param.put("accessAppCode", ApplicationConfig.getAuthorityAppCode());
		param.put("accessAppSecret", ApplicationConfig.getAuthorityPublicKey());
		String url = ApplicationConfig.getAuthorityNewUrl() + "/port/2.0/user/listDataRange.htm";
		String result = HttpUtils.sendPostQueryAndJson(url, param, JsonUtils.objectToJson(body));
		JSONObject json = JSONObject.fromObject(result);
		if(json.getInt("code") != 0) {
			log.error(json.getString("description"));
			throw new RuntimeException("查询用户数据权限失败");
		}
		List<UserRangeBean> list = new ArrayList<UserRangeBean>();
		JSONObject data = json.getJSONObject("data");
		JSONArray jsonArray = data.getJSONArray("data");
		for(int i = 0; i < jsonArray.size(); i++) {
			JSONObject object = jsonArray.getJSONObject(i);
			UserRangeBean userRange = new UserRangeBean();
			userRange.setCatalogue(object.getString("categoryCode"));
			userRange.setId(object.getString("id"));
			userRange.setUserCode(userCode);
			userRange.setValue(object.getString("code"));
			list.add(userRange);
		}
		return list;
	}

	@Override
	public void addUserRangeValue(String appId, String userCode, String catalogue, List<String> valueList) {
		if(StringUtils.isEmpty(userCode)) {
			throw new IllegalArgumentException("userCode 不能为空");
		}
		if(valueList == null || valueList.size() == 0) {
			throw new IllegalArgumentException("valueList 不能为空");
		}
		Map<String, String> param = new HashMap<String, String>();
		param.put("userCode", userCode);
		param.put("accessAppCode", ApplicationConfig.getAuthorityAppCode());
		param.put("accessAppSecret", ApplicationConfig.getAuthorityPublicKey());
		String url = ApplicationConfig.getAuthorityNewUrl() + "/port/2.0/user/linkDataRanges.htm";
		String result = HttpUtils.sendPostQueryAndJson(url, param, JsonUtils.objectToJson(valueList));
		try {
			JSONObject json = JSONObject.fromObject(result);
			if(json.getInt("code") != 0) {
				log.error(json.getString("description"));
				// throw new RuntimeException("添加用户数据权限失败");
			}
		}catch(Exception ex) {
			log.info(result);
		}
	}

	public void deleteUserRangeValue(String appId, String userCode, String catalogue, String value) {
		if(StringUtils.isEmpty(userCode)) {
			throw new IllegalArgumentException("userCode 不能为空");
		}
		if(StringUtils.isEmpty(value)) {
			throw new IllegalArgumentException("value 不能为空");
		}
		Map<String, String> param = new HashMap<String, String>();
		param.put("userCode", userCode);
		param.put("accessAppCode", ApplicationConfig.getAuthorityAppCode());
		param.put("accessAppSecret", ApplicationConfig.getAuthorityPublicKey());
		param.put("dataRangeCode", value);
		String url = ApplicationConfig.getAuthorityNewUrl() + "/port/2.0/user/unlinkDataRange.htm";
		String result = HttpUtils.sendPostForm(url, param);
		JSONObject json = JSONObject.fromObject(result);
		if(json.getInt("code") != 0) {
			log.error(json.getString("description"));
			throw new RuntimeException("删除用户数据权限失败");
		}
	}
}
